<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 17, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "base.ScriptContent" );

/**
 * Class for keeping all response information of a site.
 *
 */
class SiteResponse extends Object {
	var $_appContent = "";
	var $_arrJavascript = array ();
	var $_arrStyleSheet = array ();
	var $_arrMetaData = array ();
	var $_title;
	var $_messageQueue = array ();
	
	/**
	 * Add a Javascript File into response
	 *
	 * @param string $urlFile url to the javascript file
	 */
	function addJavascriptFile($urlFile, $media = null) {
		if (strpos ( $urlFile, 'http' ) !== 0) {
			$urlFile = SYS_ROOT_URL . $urlFile;
		}
		$script = new ScriptContent ( $urlFile, SCRIPT_TYPE_JS, SCRIPT_FILE_CONTENT, $media );
		$key = md5 ( $urlFile . "File" );
		$this->_arrJavascript [$key] = $script;
	}
	
	/**
	 * Add a Javascript content into response
	 *
	 * @param string $jsContent javascript content
	 */
	function addJavascriptContent($jsContent, $media = null) {
		$script = new ScriptContent ( $jsContent, SCRIPT_TYPE_JS, SCRIPT_TEXT_CONTENT, $media );
		$key = md5 ( $jsContent . "Content" );
		$this->_arrJavascript [$key] = $script;
	}
	
	/**
	 * Add a StyleSheet File into response
	 *
	 * @param string $urlFile url to the StyleSheet file
	 */
	function addStyleSheetFile($urlFile, $media = null) {
		if (strpos ( $urlFile, 'http' ) !== 0) {
			$urlFile = SYS_ROOT_URL . $urlFile;
		}
		$script = new ScriptContent ( $urlFile, SCRIPT_TYPE_CSS, SCRIPT_FILE_CONTENT, $media );
		$key = md5 ( $urlFile . "File" );
		$this->_arrStyleSheet [$key] = $script;
	}
	
	/**
	 * Add a StyleSheet content into response
	 *
	 * @param string $jsContent StyleSheet content
	 */
	function addStyleSheetContent($styleSheetContent, $media = null) {
		$script = new ScriptContent ( $styleSheetContent, SCRIPT_TYPE_CSS, SCRIPT_TEXT_CONTENT, $media );
		$key = md5 ( $styleSheetContent . "Content" );
		$this->_arrStyleSheet [] = $script;
	}
	
	/**
	 * Add the meta data for response
	 *
	 * @param string $name
	 * @param string $content
	 */
	function addMetaData($name, $content) {
		$this->_arrMetaData [$name] = $content;
	}
	
	/**
	 * Remove a metadata out of render 
	 * @param $name
	 */
	function removeMetaData($name) {
		$this->_arrMetaData[$name] = null;
		unset($this->_arrMetaData[$name]);
	}
	
	/**
	 * Append the meta data for response
	 *
	 * @param string $name
	 * @param string $content
	 */
	function appendMetaData($name, $content) {
		$this->_arrMetaData [$name] .= $content;
	}
	
	/**
	 * Prepend the meta data for response
	 *
	 * @param string $name
	 * @param string $content
	 */
	function prependMetaData($name, $content) {
		$this->_arrMetaData [$name] = $content.$this->_arrMetaData [$name];
	}
	
	/**
	 * Append a string into content of respond
	 *
	 * @param string $appendedStr
	 */
	function appendAppContent($appendedStr) {
		$this->_appContent .= $appendedStr;
	}
	
	/**
	 * Add the input string before the current appContent
	 *
	 * @param string $prependString
	 */
	function prependAppContent($prependString) {
		$this->_appContent = $prependString . $this->_appContent;
	}
	
	/**
	 * Set the application content for this respond
	 *
	 * @param string $appContent
	 */
	function setAppContent($appContent) {
		$this->_appContent = $appContent;
	}
	
	/**
	 * Set the title of respond
	 *
	 * @param string $title
	 */
	function setPageTitle($title) {
		$this->_title = $title;
	}
	
	/**
	 * Add the input string before the current page tile
	 *
	 * @param string $prependString
	 */
	function prependPageTitle($prependString) {
		$this->_title = $prependString . $this->_title;
	}
	
	/**
	 * Append the current title with the input string.
	 * Ex: Current title: "Framework tile"
	 * 		Input string: " | Hello World"
	 * 	-> new title string: "Framework tile | Hello World"
	 *  
	 * @param string $string
	 */
	function appendPageTitle($string) {
		$this->_title .= $string;
	}
	
	/**
	 * Get all header information inside respond then print it.
	 * Header information include:
	 * 		+	Title
	 * 		+ 	Meta Data
	 * 		+	JavaScript
	 * 		+	StyleSheet
	 *
	 * @return string
	 */
	function getHeaderHtml($includeTitle = true) {
		if ($includeTitle) {
			$result = "<title>{$this->_title}</title>\n";
		}
		foreach ( $this->_arrMetaData as $key => $value ) {
			$result .= "<meta http-equiv=\"{$key}\" name=\"{$key}\" content=\"{$value}\" />\n";
		}
		foreach ( $this->_arrJavascript as $script ) {
			$result .= $script->getHtml ();
		}
		foreach ( $this->_arrStyleSheet as $script ) {
			$result .= $script->getHtml ();
		}
		return $result;
	}
	
	/**
	 * Clear all header information inside
	 *
	 */
	function clearAllHeaderInfo() {
		$this->_arrMetaData = array ();
		$this->_arrJavascript = array ();
		$this->_arrStyleSheet = array ();
	}
	
	/**
	 * Echo all header information inside respond then print it.
	 * Header information include:
	 * 		+	Title
	 * 		+ 	Meta Data
	 * 		+	JavaScript
	 * 		+	StyleSheet
	 *
	 */
	function printHeaderHtml() {
		echo $this->getHeaderHtml ();
	}
	
	/**
	 * Get the application content of Respond
	 *
	 * @return string
	 */
	function getAppContent() {
		return $this->_appContent;
	}
	
	/**
	 * Echo the application content of Respond
	 *
	 */
	function printAppContent() {
		echo $this->getAppContent ();
	}
	
	/**
	 * Enqueue a system message.
	 *
	 * @access	public
	 * @param	string 	$msg 	The message to enqueue.
	 * @param	string	$type	The message type.
	 * @return	void
	 */
	function enqueueMessage($msg, $type = 'message') {
		// Enqueue the message
		$this->_messageQueue [] = array ('message' => $msg, 'type' => strtolower ( $type ) );
	}
	
	/**
	 * Get the message list
	 *
	 * @return array
	 */
	function &getMessages() {
		return $this->_messageQueue;
	}

}
?>
